
/*
 * @features: 地图绘制显示
 * @description: 控制地图的显示和绘制
 * @Date: 2020-10-14 21:04:46
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2020-11-22 23:22:15
 * @LastEditors: judu233
 */

import { I_DrawInfo, I_LinePoint, I_MapButtonData, I_MapData, I_MapDrawEvent, I_Pos } from "./IMapType";
import MapContrl from "./MapContrl";
import z from "../../Manager/Z";


const { ccclass, property } = cc._decorator;



/**  
    * @Title : 地图显示
    * @Description : 类说明
 **/
@ccclass
export default class MapDraw extends cc.Component implements I_MapDrawEvent {
    /**地图逻辑控制 */
    public mapContrl: MapContrl;

    @property(cc.Prefab)
    public btnPrefab: cc.Prefab = null;

    zz__start() {
        //test 初始化一个4x4地图占位类型
        let mapData = [
            //0  1  2  3
            [1, 1, 1, 0], //0
            [1, 0, 1, 0], //1
            [1, 1, 0, 0],
            [1, 1, 1, 1],
        ];

        this.mapContrl = new MapContrl({
            dataArr: mapData,
            name: `测试`,
            id: 1,
            startPos: { x: 0, y: 0 },
            endPos: { x: 3, y: 3 },
            centerPos: { x: 3, y: 3 },//还有bug
            height: 150,
            width: 150,
        });
        this.creatorMap();
        this.mapContrl.setBtnRelationship({ x: 1, y: 0 }, { x: 1, y: 2 }, false);
        z.camera.initCamera(this.mapContrl.data.nowClickBtn.btn.node.position);
    }

    public saveBtn() {
        this.mapContrl.saveData();
    }

    public loadBtn() {
        this.mapContrl = new MapContrl();
        this.mapContrl.loadData();
        this.drawMap(this.mapContrl.getLoadDrawInfo());
        let firstBtnInfo = this.mapContrl.data.nowClickBtn;
        firstBtnInfo.btn.node.active = true;
        this.firstBtnInit(firstBtnInfo.btn);
        z.camera.initCamera(firstBtnInfo.btn.node.position);
    }

    public creatorMap() {
        //获取第一个按钮并显示
        let firstBtnInfo = this.mapContrl.data.nowClickBtn;
        firstBtnInfo.btn.node.active = true;
        //初始化第一个按钮
        this.firstBtnInit(firstBtnInfo.btn);
        //绘制第一个按钮可以点击范围
        this.drawMap(this.mapContrl.getNextDrawInfo(firstBtnInfo.btn));
    }

    /**
     * 初始化第一个按钮
     * @param firstBtn 第一个按钮
     */
    public firstBtnInit(firstBtn: cc.Button) {
        this.setBtnLabel(firstBtn, `开始`);
    }

    /**
     * 设置按钮的文本
     * @param btnInfo  按钮或按钮信息
     * @param str 要设置的值
     */
    public setBtnLabel(btnInfo: I_MapButtonData | cc.Button, str: string) {
        let btn: cc.Button = btnInfo instanceof cc.Button ? (btnInfo as cc.Button) : (btnInfo as I_MapButtonData).btn;
        btn.node.getComponentInChildren(cc.Label).string = str;
    }

    /**
     * 根据绘制信息在地图上绘制按钮组和线条组
     * @param drawInfo 绘制信息
     */
    public drawMap(drawInfo: I_DrawInfo) {
        if (is.undefined(drawInfo)) return;

        //绘制
        let { btnInfoList, lineList } = drawInfo;
        this.scheduleOnce(() => {
            this.drawLine(lineList);
        }, 0.2);
        this.scheduleOnce(() => {
            this.drawBtn(btnInfoList);
        }, 0.35);
    }

    /**
     * 绘制线条
     * @param lineList 要绘制线条的列表
     */
    private drawLine(lineList: I_LinePoint[]) {
        lineList.forEach((lineInfo, index) => {
            z.drawComList[index].drawLine(lineInfo);
        });
    }

    /**
     * 绘制按钮
     * @param btnInfoList 要绘制按钮的信息列表
     */
    private drawBtn(btnInfoList: I_MapButtonData[]) {
        btnInfoList.forEach(btnInfo => {
            btnInfo.btn.node.active = true;
            this.setBtnLabel(btnInfo, `${btnInfo.x}_${btnInfo.y}`);
        });
    }

    /**mapContrl获取按钮 */
    public creatorBtn() {
        return cc.instantiate(this.btnPrefab)?.getComponent(cc.Button);
    }

    /**
     *  点击生成的按钮
     * @param event 点击按钮事件
     */
    public btnClickEvent(event: cc.Event) {
        let btn = event.target.getComponent(cc.Button) as cc.Button;
        //触发地图接下的绘制操作
        let isClick = this.mapContrl.clickBtn(event, btn);
        //更新摄像机位置
        isClick && z.camera.updateCamera(btn.node.position, this.mapContrl.data.lastClickBtn.btn.node.position);
    }
}
